package com.wanxs.mall.product.controller;

import com.wanxs.mall.common.annotation.LogOperation;
import com.wanxs.mall.common.constant.Constant;
import com.wanxs.mall.common.exception.groups.UpdateGroupInterface;
import com.wanxs.mall.common.page.PageData;
import com.wanxs.mall.common.utils.ExcelUtils;
import com.wanxs.mall.common.utils.PageUtils;
import com.wanxs.mall.common.utils.R;
import com.wanxs.mall.common.utils.Result;
import com.wanxs.mall.common.validator.AssertUtils;
import com.wanxs.mall.common.validator.ValidatorUtils;
import com.wanxs.mall.common.validator.group.DefaultGroup;
import com.wanxs.mall.common.validator.group.UpdateGroup;
import com.wanxs.mall.product.dto.BrandDTO;
import com.wanxs.mall.product.entity.BrandEntity;
import com.wanxs.mall.product.excel.BrandExcel;
import com.wanxs.mall.product.service.BrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 品牌
 *
 * @author wanxs 130001934@qq.com
 * @since 1.0.0 2023-09-01
 */
@RestController
@RequestMapping("product/brand")
@Api(tags="品牌")
public class BrandController {
    @Autowired
    private BrandService brandService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("product:brand:page")
    public Result<PageData<BrandDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){
        PageData<BrandDTO> page = brandService.page(params);

        return new Result<PageData<BrandDTO>>().ok(page);
    }

    @GetMapping("queryPage")
    @ApiOperation("分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
            @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
            @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String")
    })
    @RequiresPermissions("product:brand:page")
    public R queryPage(@ApiIgnore @RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("data",page);
    }


    @GetMapping("{id}")
    @ApiOperation("信息")
    @RequiresPermissions("product:brand:info")
    public Result<BrandDTO> get(@PathVariable("id") Long id){
        BrandDTO data = brandService.get(id);

        return new Result<BrandDTO>().ok(data);
    }

    @PostMapping("save")
    @ApiOperation("保存")
    @LogOperation("保存")
//    @RequiresPermissions("product:brand:save")
    public R save( @Valid @RequestBody BrandDTO dto, BindingResult result) throws MethodArgumentNotValidException {
        //效验数据
//        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        if(result.hasErrors()){
            // 提交的数据经过JSR303校验后有非法的字段
//            Map<String,String> map = new HashMap<>();
//            List<FieldError> fieldErrors = result.getFieldErrors();
//            for (FieldError fieldError : fieldErrors) {
//                // 获取非法数据的 field
//                String field = fieldError.getField();
//                // 获取非法的field的提示信息
//                String defaultMessage = fieldError.getDefaultMessage();
//                map.put(field,defaultMessage);
//            }
            throw new MethodArgumentNotValidException(null,result);

        }
        brandService.save(dto);

        return R.ok();
    }

    @PostMapping("update")
    @ApiOperation("修改")
    @LogOperation("修改")
//    @RequiresPermissions("product:brand:update")
    public Result update(@Validated(UpdateGroupInterface.class) @RequestBody BrandEntity dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        brandService.updateDetail(dto);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("product:brand:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        brandService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @ApiOperation("导出")
    @LogOperation("导出")
    @RequiresPermissions("product:brand:export")
    public void export(@ApiIgnore @RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<BrandDTO> list = brandService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, list, BrandExcel.class);
    }

    @GetMapping("/all")
    public R queryAllBrand(){
        Map param = new HashMap<String ,Object>();
        List<BrandDTO> list = brandService.list(param);
        return R.ok().put("brands",list);
    }


}